import {
  SET_PHONE,
  SET_START_TIME,
  SET_END_TIME,
  GET_DATA,
  GET_DATA_SUCCESS,
  GET_DATA_FAIL,
  SET_PAGE_NUM,
  SHOW_ADD_MODAL,
  HIDDEN_ADD_MODAL,
  SET_ADD_PHONE,
  ADD_DATA,
  ADD_DATA_SUCCESS,
  ADD_DATA_FAIL,
  DELETE_DATA,
  DELETE_DATA_SUCCESS,
  DELETE_DATA_FAIL,
} from '@/actions/whiteList'
import util from '@/utils';

const initState = {
  phone: '',
  startTime: null,
  endTime: null,
  pageNum: 1,
  pageSize: 10,
  total: 0,
  loading: false,
  data: [],
  isAddModalShow: false,
  addButtonLoading: false,
  addPhone: '',
}

export default function whiteListReducer(state = initState, action) {
  switch (action.type) {

    case SET_PHONE:
      return { ...state, phone: action.phone }
      break;

    case SET_START_TIME:
      return { ...state, startTime: util.getStartTime(action.startTime) }
      break;

    case SET_END_TIME:
      return { ...state, endTime: util.getEndTime(action.endTime) }
      break;

    case GET_DATA:
      return { ...state, loading: true }
      break;

    case GET_DATA_SUCCESS:
      const { list, total } = action.data
      return { ...state, data: list, total, loading: false }
      break;

    case GET_DATA_FAIL:
      return { ...state, loading: false }
      break;

    case SHOW_ADD_MODAL:
      return { ...state, isAddModalShow: true }
      break;

    case SET_PAGE_NUM:
      return { ...state, pageNum: action.pageNum }
      break;

    case HIDDEN_ADD_MODAL:
      return { ...state, isAddModalShow: false, addPhone: ''  }
      break;

    case SET_ADD_PHONE:
      return { ...state, addPhone: action.addPhone }
      break;

    case ADD_DATA:
      return { ...state, addButtonLoading: true }
      break;

    case ADD_DATA_SUCCESS:
      return { ...state, addButtonLoading: false, isAddModalShow: false }
      break;

    case ADD_DATA_FAIL:
      return { ...state, addButtonLoading: false }
      break;

    case DELETE_DATA:
      return { ...state, loading: true }
      break;

    case DELETE_DATA_SUCCESS:
      return { ...state, loading: false }
      break;

    case DELETE_DATA_FAIL:
      return { ...state, loading: false }
      break;
  
    default:
      return state
      break;
  }
}